package test;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.*;

/**
 *@author Administrator
 *@date Jan 18, 2010
 */
@Stateless
public class UploadToDB implements IImgUploadService{

	@PersistenceContext(unitName="stuts2PU")
	private EntityManager em;
	private EntityManagerFactory emf;
	
	public UploadToDB(){
		emf=Persistence.createEntityManagerFactory("stuts2PU");
		em=emf.createEntityManager();
	}
	
	public void delete(Integer id) {
		em.remove(findById(id));
	}

	@SuppressWarnings("unchecked")
	public List<Imgupload> findAll() {
		String sql="select c from Imgupload c ";
		Query query=em.createNamedQuery(sql);
		List<Imgupload> list=query.getResultList();
		return list;
	}

	public Imgupload findById(Integer id) {
		return em.find(Imgupload.class, id);
	}


	public void save(Imgupload img) {
		em.getTransaction().begin();
		em.persist(img);	
		em.getTransaction().commit();
		em.close();
		emf.close();
	}

	/**
	 * ${see_to_overridden}
	 **/
	public void update(Imgupload img) {
		em.merge(img);		
	}

}
